import { defineStore } from "pinia";
import { UserState } from "@/stores/interface";
import piniaPersistConfig from "@/stores/helper/persist";
import { loginApi } from "@/api/modules/login";

export const useUserStore = defineStore({
  id: "geeker-user",
  state: (): UserState => ({
    token: "",
    staffCode: "",
    staffName: "",
    source: ""
  }),
  getters: {},
  actions: {
    // Set Token
    setToken(token: string) {
      this.token = token;
    },
    // Set setUserInfo
    setUserInfo(staffName: UserState["staffName"], staffCode: UserState["staffCode"], source: UserState["source"]) {
      this.staffName = staffName;
      this.staffCode = staffCode;
      this.source = source;
    },
    // Clear localStorage data
    clearLocalStorage() {
      localStorage.removeItem("geeker-user");
    },
    // Get AuthTokenList
    async getAuthTokenList(staffName: string, staffCode: string, source: string) {
      const { data } = await loginApi({ staffName, staffCode, source });
      this.token = data.access_token;
    }
  },
  persist: piniaPersistConfig("geeker-user", ["token", "staffName", "staffCode"], sessionStorage)
});
